<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Description of commoncall
 *

 */
class Commoncall extends CI_Model {

	//put your code here
	public $loc;

	function __construct() {
		parent::__construct();
		log_message('debug', 'Model Initialized');
$site = 	$_SERVER['HTTP_HOST'];
		//die($site);
		
			if ($site != trim(strtolower('www.aummediagroup.com')) && 
			$site != trim(strtolower('aummediagroup.com')) && 
			$site != trim(strtolower('localhost'))  && 
			$site != trim(strtolower('http://www.aummediagroup.com'))) {
            $error = 'Sorry! Not Allowed to ' . $site;
            $error .= '</BR><b>www.yellowpagesphpscript.com';
            $error .= '</BR>infolinematrix@gmail.com</b>';

           print $error;
		
           die();
		}
	}






	function get_ip_location($ip, $format="xml") {

		/* Set allowed output formats */
		$formats_allowed = array("json", "xml", "raw");

		/* IP location query url */
		$query_url = "http://iplocationtools.com/ip_query.php?ip=";

		/* Male sure that the format is one of json, xml, raw.
		 Or else default to xml */
		if(!in_array($format, $formats_allowed)) {
			$format = "xml";
		}

		$query_url = $query_url . "{$ip}&output={$format}";

		/* Init CURL and its options*/
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $query_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 15);

		/* Execute CURL and get the response */
		return curl_exec($ch);

	}

	function ipCity(){
		$location_data = $this->get_ip_location("209.85.153.104");

		$data = '';

		try {
			$data = @new SimpleXMLElement($location_data);
		}
		catch(Exception $e) {
			echo "Error parsing XML.";
		}

		foreach($data as $key=>$value)
			echo $key . " : " . $value . "\n";

	}

	function getCurrent_url(){
		$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
		if ($_SERVER["SERVER_PORT"] != "80")
		{
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		}
		else
		{
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
		//--//
	}

	function get_client_ip() {
		$ipaddress = '';
		if ($_SERVER['HTTP_CLIENT_IP'])
			$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
		else if($_SERVER['HTTP_X_FORWARDED_FOR'])
			$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
		else if($_SERVER['HTTP_X_FORWARDED'])
			$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
		else if($_SERVER['HTTP_FORWARDED_FOR'])
			$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
		else if($_SERVER['HTTP_FORWARDED'])
			$ipaddress = $_SERVER['HTTP_FORWARDED'];
		else if($_SERVER['REMOTE_ADDR'])
			$ipaddress = $_SERVER['REMOTE_ADDR'];
		else
			$ipaddress = 'UNKNOWN';

		return $ipaddress;
	}


	public function get_id($strUrl){
		if(!$strUrl){
			return 0;
		}

		if($strUrl){
			$len=strlen($strUrl);
			$pos=strrpos($strUrl, '-');
			$id= substr($strUrl,$pos+1);
			return $id;
		}

	}
	//--Set Data Format ---//
	function date_format($format='d-M-Y',$strDate){
		$dt=date($format, strtotime($strDate));
		return $dt;
	}

	//--Set URL friendly --//
	function url($strUrl){
		$url = url_title(strtolower($strUrl));
		return $url;
	}

	function random_text($lenght=Null) {
		$this->load->helper('string');
		return random_string('alnum', $lenght);
	}
	function send_mail($email = NULL, $subject = NULL, $body = NULL) {

		$this->email->clear();
		//--Email Config
		$email_config['protocol'] = 'mail';
		$email_config['wordwrap'] = FALSE;
		$email_config['mailtype'] = 'html';
		$email_config['charset'] = 'utf-8';
		$email_config['crlf'] = "\r\n";
		$email_config['newline'] = "\r\n";
		$this->email->initialize($email_config);

		$this->email->to($email);
		$this->email->from($this->settings_model->get_web_configuration('default_email'), $this->settings_model->get_web_configuration('web_title'));
		$this->email->subject($subject);
		$this->email->message($body);
		$this->email->send();
	}

	//--Currency Format --//
	function currency($amount="00"){
		setlocale(LC_MONETARY, 'en_IN');
		$amount = money_format('%!i', $amount);
		return $amount;
	}


	function upload($imageData=NULL){
	
		if ($imageData['imagearray']['error'] == 0) {
	
			$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $imageData['imagearray']['name']);
			$imagename=strtolower($imageData['imagearray']['name']);
			$imagename=explode( '.',$imagename);
			$imagename=$imagename[0];
	
			if($imageData['customefilename']){
				$filename=$imageData['customefilename'].'.'.$ext;
			}
			else{
				$filename = $imagename.'.'.$ext;
			}
	
			if($imageData['namewithtime']=='Yes'||$imageData['namewithtime']=='yes'){
				$filename=$imagename.time().'_thumb.'.$ext;
				if($imageData['separator'])
					$filename=$imagename.$imageData['separator'].time().'_thumb.'.$ext;
			}
			if($imageData['prefix'])
				$filename=$imageData['prefix'].$filename;
	
			$config['file_name'] = strtolower($filename);
			$config['upload_path'] = $imageData['imagepath'];
			$config['allowed_types'] = $imageData['allowedtype'];
			$config['overwrite'] = $imageData['overwrite'];
			$config['remove_spaces'] = $imageData['removespace'];
	
	
			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('userfile')) {
	
				if($imageData['errmessage'])
					$imagedata=array('message'=>$imageData['errmessage'],'data'=>"" ,'code'=>0);
				else
					$imagedata=array('message'=>$this->upload->display_errors(),'data'=>"",'code'=>0);
				return $imagedata;
			}
			//if sucessfull
	
	
	
			else{
				if($imageData['createthumb']=='Yes'||$imageData['createthumb']=='yes')
				{
					//print $filename;
					//die();
					$thumb['image_library'] = 'gd2';
					$thumb['source_image'] = $imageData['imagepath'].$filename;
					$thumb['new_image'] = $imageData['thumbpath'];
					$thumb['create_thumb'] = true;
					$thumb['maintain_ratio'] = false;
					$thumb['thumb_marker']  = '_thumb' ;
					$thumb['width'] = $imageData['thumbwidth'];
					$thumb['height'] = $imageData['thumbheight'];
	
					$this->load->library('image_lib', $thumb);
					$this->image_lib->resize();
					if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
					$this->image_lib->clear();
				}
				if($imageData['sucessmessage'])
					$imagedata=array('message'=>$imageData['sucessmessage'],'data'=>$filename,'code'=>1);
				else
					$imagedata=array('message'=>"Image Uploded Sucesfully...",'data'=>$filename,'code'=>1);
				return $imagedata;
			}
		}
	}
	
	function clean_url($url=''){
		//print $url;
		$str = $url;
		$str = preg_replace('#^https?://#', '', $str);
		return $str; // www.google.com
	
	}
	
	function get_ip() {
		if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} else {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
	
	function getModule($name){
		$sql = "Select status from modules where module = '$name'";
		$query = $this->db->query($sql);
		$result = $query->row();
		if ($query->num_rows() > 0){
			return $result->status;
		} else {
			return 0;
		}
		
	}
	
	function getModuleSlider($name){
		$sql = "Select status from modules where module = '$name'";
		$query = $this->db->query($sql);
		$result = $query->row();
		if ($query->num_rows() > 0){
			return $result->status;
		} else {
			return 0;
		}
	
	}
}

?>
